<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\ProductArea;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;
use app\services\Chain;

class Up
{
    protected $order;
    protected $user;

    protected $set;

    protected $first_order_item;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();

        $this->first_order_item = $first_order_item;
        $product_id = $first_order_item->product_id;
        $this->set = SetUserUp::getLastOne();
        $is_bao = ProductArea::checkBao($product_id);

        // 必须是报单区
        if ($is_bao) {
            $this->upMyself();
            $this->upChain();
        }

    }


    // 升级自己
    protected function upMyself()
    {
        if ($this->first_order_item->price >= $this->set->vip3) {

            // 配额
            $this->user->has_vip1 += $this->set->vip3_has_vip1;
            $this->user->save();
            $this->user->has_vip2 += $this->set->vip3_has_vip2;
            $this->user->save();


            if ($this->user->level < UserLevel::$keys['vip3']) {
                $this->user->level = UserLevel::$keys['vip3'];
                $this->user->save();
            }
        }

        if ($this->first_order_item->price >= $this->set->vip2) {
            // 配额
            $this->user->has_vip1 += $this->set->vip2_has_vip1;
            $this->user->save();

            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();
            }
        }

        if ($this->first_order_item->price >= $this->set->vip1) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();
            }
        }

    }

    // 升级关系链
    protected function upChain()
    {
        // 只需要处理VIP1, VIP2的
        $chain = Chain::getReferrerChain($this->user->id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
        ], true);

        foreach ($chain as $user) {
            if ($user->level < UserLevel::$keys['vip3']) {
                $count = Chain::getChildrenLevel($user->id, [
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                ]);
                $count = count($count);
                if ($count >= $this->set->vip3_2) {
                    $user->level = UserLevel::$keys['vip3'];
                    $user->save();
                }
            }

            if ($user->level < UserLevel::$keys['vip2']) {
                $count = Chain::getChildrenLevel($user->id, [
                    UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                ]);
                $count = count($count);
                if ($count >= $this->set->vip3_2) {
                    $user->level = UserLevel::$keys['vip2'];
                    $user->save();
                }
            }

        }
    }


}